package com.atguigu.spring.aop.proxy.dynamic;

import com.atguigu.spring.aop.log.LogUtils;

import java.lang.reflect.Proxy;

/**
 * JDK动态代理的目标对象必须有定义接口
 */
public class DynamicProxy {

    public static Object getProxyInstance(Object target) {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    LogUtils.logStart(method, args);
                    Object result = null;
                    try {
                        result = method.invoke(target, args);

                        LogUtils.logResult(method, result);
                    } catch (Exception e) {
                        LogUtils.logException(method, e);
                    } finally {
                        LogUtils.logEnd(method);
                    }
                    return result;
                }
        );
    }
}
